package com.example.demo8;

public class CollisionSystem {
    private int[][] room;
    private final int tileSize;

    public CollisionSystem(int[][] room, int tileSize) {
        this.room = room;
        this.tileSize = tileSize;
    }

    // 墙壁碰撞检测
    public boolean checkWallCollision(double x, double y, double radius) {
        int gridX = (int) (x / tileSize), gridY = (int) (y / tileSize);

        // 扩展碰撞范围，检查玩家是否和墙壁的任何部分发生重叠
        for (int dx = -1; dx <= 1; dx++) {
            for (int dy = -1; dy <= 1; dy++) {
                int checkX = gridX + dx, checkY = gridY + dy;

                if (checkX >= 0 && checkX < room.length &&
                        checkY >= 0 && checkY < room[0].length &&
                        room[checkX][checkY] == 1) {  // 检查墙壁

                    // 获取墙壁的四个角的坐标
                    double wallX = checkX * tileSize;
                    double wallY = checkY * tileSize;

                    // 检查玩家是否与墙壁的某个区域重叠
                    if (Math.abs(x - (wallX + tileSize / 2)) < radius + tileSize / 2 &&
                            Math.abs(y - (wallY + tileSize / 2)) < radius + tileSize / 2) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    // 对象间碰撞检测
    public boolean checkEntityCollision(double x1, double y1, double r1,
                                        double x2, double y2, double r2) {
        return Math.hypot(x1 - x2, y1 - y2) < r1 + r2;
    }

    // 更新房间数据（当房间变化时调用）
    public void updateRoom(int[][] newRoom) {
        this.room = newRoom;
    }
}